package com.xueda.flu_framework.pay;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

import com.alipay.sdk.app.PayTask;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;

public class PayPlugin implements FlutterPlugin, MethodChannel.MethodCallHandler {

    private static final String CHANNEL_METHOD_PAY = "channel_method_pay";
    private Context mContext;

    private ExecutorService executorService;

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            Log.e("ah zfb",msg.toString());
        };
    };

    public PayPlugin(Context context) {
        this.mContext = context;
        executorService = Executors.newCachedThreadPool();
    }

    @Override
    public void onAttachedToEngine(@NonNull FlutterPluginBinding binding) {
        final MethodChannel methodChannel = new MethodChannel(binding.getBinaryMessenger(),CHANNEL_METHOD_PAY);
        methodChannel.setMethodCallHandler(this);
    }

    @Override
    public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {

    }

    @Override
    public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
        switch (call.method) {
            case"wxPay":
                wxPay((Map) call.arguments);
                break;
            case"zfbPay":
                zfbPay((Map) call.arguments);
                break;
        }
    }

    private void zfbPay(Map argsMap) {
        final String orderInfo = "info";   // 订单信息
        Log.e("ah","pay info == "+(String) argsMap.get("info"));
        Runnable payRunnable = new Runnable() {
            @Override
            public void run() {
                PayTask alipay = new PayTask((Activity)mContext);
                Map <String,String> result = alipay.payV2(orderInfo,true);
                Message msg = new Message();
//                msg.what = SDK_PAY_FLAG;
                msg.what = 1;
                msg.obj = result;
                mHandler.sendMessage(msg);
            }
        };
        executorService.execute(payRunnable);
    }

    private void wxPay(Map argsMap){
        Log.e("ah","pay info == "+(String) argsMap.get("info"));

        IWXAPI api = WXAPIFactory.createWXAPI(mContext, "mAppId");
        PayReq request = new PayReq();
        request.appId = "wxd930ea5d5a258f4f";
        request.partnerId = "1900000109";
        request.prepayId= "1101000000140415649af9fc314aa427";
        request.packageValue = "Sign=WXPay";
        request.nonceStr= "1101000000140429eb40476f8896f4c9";
        request.timeStamp= "1398746574";
        request.sign= "7FFECB600D7157C5AA49810D2D8F28BC2811827B";
        api.sendReq(request);
    }



}
